<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize.js'
import { sevenRecord } from '@/api/topic'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      chartData: {
        acData: [100, 120, 161, 134, 105, 160, 165],
        submitData: [120, 82, 91, 154, 162, 140, 145]
      },
      date: ['04-03', '04-04', '04-05', '04-06', '04-07', '04-08', 'N']
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    sevenRecord().then(res => {
      this.chartData.acData = res.data.map(t => t.ac)
      this.chartData.submitData = res.data.map(t => t.submit)
      this.date = res.data.map(t => t.date.substring(5))
      this.$nextTick(() => {
        this.initChart()
      })
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ acData, submitData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.date,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['AC', 'Submit']
        },
        series: [{
          name: 'AC', itemStyle: {
            normal: {
              color: '#2ecc71',
              lineStyle: {
                color: '#2ecc71',
                width: 2
              }
            }
          },
          smooth: true,
          type: 'line',
          data: acData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        },
        {
          name: 'Submit',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#3888fa',
              lineStyle: {
                color: '#3888fa',
                width: 2
              },
              areaStyle: {
                color: '#f3f8ff'
              }
            }
          },
          data: submitData,
          animationDuration: 2800,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
